(function(){
	var data = {};
	var filesetting = {
		//上传请求的url
		action: 'upload.php',
		name: 'userfile',
		//请求的参数，可以通过setData改变
		data: {},
		autoSubmit: true,
		//请求类型，如json
		responseType: false,
		onSubmit: function(filename, ext) {
			if (this._settings.extFormat) {
				if(!(ext && eval('/^('+this._settings.extFormat+')$/i').test(ext))){
					alert('未允许上传的文件格式!');
					return false;
				}
			}
			$.dialog.tips('上传中，请稍候...',600,'loading.gif');
		},
		onComplete: function(filename, data) {
			var code = data.getElementsByTagName('code')[0].firstChild.data;
			var desc = data.getElementsByTagName('desc')[0].firstChild.data;
			//var code = data.result;
			//var desc = data.msg;
			switch(parseInt(code)){
				case 0:
					data=jQuery.parseJSON(desc);
					var $button = $(this._button);
					var $span = $button.sibling('span#filelist');
					if($span.length==0){
						$span = $('<span id="filelist"></span>');
						$span.after($button);
					}
					//data = data.info;
					$.dialog.tips(desc,1,'success.gif');
					var downloadHtml = this._settings.getDownloadHtml();
					var delHmtl = this._settings.getDelHtml();
					var $delHtml = $(delHtml);
					$delHtml.click(function(){
						this._settings.delFile();
					});
					$("#"+tar+" .fileDownload").attr("href",toUrl.replaceAll("upload","download")+"&uuid="+data.sid);
					$("#"+tar+" .fileDownload").text("下载");
					$("#"+tar+" .fileDel").attr("href","javascript:delFile('"+data.sid+"','"+toUrl.replaceAll("upload","del")+"','"+tar+"')");
					$("#"+tar+" .fileDel").text('删除');
					$("#"+uuid).val(data.sid);
					$.cssTable.focus().submit();
					break;
				default:
					$.dialog.tips(desc,0);
					$.dialog.alert(desc);
					break;
			}
		},
		//如果想要保存ajaxupload对象的话，需要制定一个唯一id，以后可以通过FileUpload.getFileUpload(id)获取
		id:null,
		//上传按钮，如果是onclick="FileUpload({})" 或者 $('#uploadbutton').FileUpload({})来调用，则可忽略
		button:null,
		maxSize:null,
		fileCount:null,
		extFormat:null,
		getDownloadHtml:function(){
			var url = this._settings.action||'';
			var data = this._settings.data;
			if(data){
				if(url.indexOf('?')==-1){url += '?';}
				for(var i in data){
					url += '&'+i+'='+data[i];
				}
			}
			url = url.replace('upload','download');
			var html = '<a class="fileDownload" href="'+url+'">下载</a>';
			return html;
		},
		getDelHtml:function(){
			var html = '<a class="fileDel" href="javascript:;">删除</a>';
			return html;
		},
		//删除附件
		delFile: function(url,pars,el){
			$.dialog.confirm('你确定要删除该附件吗？', function(){
				$.post(url,pars,function(data){
							switch(data.result){
								case 0:
							       $.dialog.tips(data.msg,1,'success.gif');
								   $.cssTable.focus().submit();
								   break;
								default:
									$.dialog.alert(data.msg);
									break;
							}
						},'json');
					}, function(){
			    return;
			 });
		},
		//附件是否是图片
		isImage:false,
		//预览图片
		viewImage:function(){
			var $that = $(that);
		    $.dialog({
		        id: '_blank', 
		        title:'图片预览',				
		        lock:true,
		        padding: 0,
		        content:'<img src="' +$that.attr("src")+ '" width="500" height="400" />'
		    });
		},
		//如果图片不存在
		imageError:function(obj){
			try{
				//var obj = event.srcElement;
				obj.onerror=null;
				obj.src='./acl/common/upload/skins/small_logo.jpg';
				obj.title='未上传';
			}catch(ex){}
		}
	};
	var FileUpload = function(options){
		if(!AjaxUpload){return;}
		if(!options){return;}
		var el = tool.getEl(),el = el==null?this:el;
		var oNode = options.button||el;
		if(!oNode || oNode==window){return;}
		var dfmaxsize ,dffilecount,dfuploadformat;
		if(typeof oNode == 'string'){
			oNode = JSON.parse(oNode);
			options.maxSize = oNode['dfmaxsize']||20;
			options.fileCount = oNode['dffilecount']||1;
			options.extFormat = oNode['dfuploadformat'];
		}else{
			if(oNode.jquery) {
				oNode = oNode[0];
			}
			options.maxSize = oNode.getAttribute('dfmaxsize')||20;
			options.fileCount = oNode.getAttribute('dffilecount')||1;
			options.extFormat = oNode.getAttribute('dfuploadformat');
		}
		AjaxUpload.prototype.isImage=function(b){
			this._settings.isImage=b||false;
		}
		AjaxUpload.prototype.getFileUpload= function(){
			return this;
		}
		if(options){
			$.extend(true,filesetting,options);
		}
		var _ = new AjaxUpload(oNode, filesetting);
		if(_._settings.id){
			data[_._settings.id] = _;
		}
		return _;
	};
	FileUpload.getFileUpload= function(id){
		return data[id];
	}
	$.prototype.FileUpload = window.FileUpload = FileUpload;
	var tool = {
		getEvent : function() {
			var appName = navigator.appName;
			var not_IE_Opera = appName != "Microsoft Internet Explorer"
					&& appName != "Opera";
			if (not_IE_Opera) {
				func = this.getEvent.caller;
				var deep = 0;
				var max = 10;
				while (func != null && deep<max) {
					var $ = func.arguments[0];
					if ($ && ($ + "").indexOf("Event") >= 0)
						return $;
					func = func.caller;
					deep++;
				}
				return null;
			}
			return event;
		},
		getEl : function() {
			var event = this.getEvent();
			if(!event) return null;
			return event.srcElement || event.target;
		}
	};
})();
